package org.wzl.service.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import jakarta.annotation.Resource;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import org.wzl.service._do.UserInfo;
import org.wzl.service._do.UserLogin;
import org.wzl.service.bo.UserDetail;
import org.wzl.service.bo.redis.RedisUserBO;
import org.wzl.service.exception.BusinessException;
import org.wzl.service.mapper.UserInfoMapper;
import org.wzl.service.mapper.UserLoginMapper;
import org.wzl.service.service.UserLoginService;

/**
 * @author: 卫志龙
 * @date: 2024年06月29日 10:58
 */
@Service
public class UserDetailServiceImpl implements UserDetailsService {

    @Resource
    private UserLoginMapper userLoginMapper;

    @Resource
    private UserInfoMapper userInfoMapper;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        UserLogin login = userLoginMapper.selectOne(Wrappers.<UserLogin>lambdaQuery()
                .eq(UserLogin::getUsername, username));

        if (null == login) {
            throw new BusinessException("用户名:" + username + ",不存在");
        }

        UserInfo userInfo = userInfoMapper.selectById(login.getId());

        return new UserDetail(new RedisUserBO(userInfo, login));
    }

}
